package com.silent.socktest;

import java.io.*;
import java.net.*;

public class SocketServer {
	ServerSocket sever;
    
    public SocketServer(int port){
        try{
            sever = new ServerSocket(port);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    public void beginListen(){
        while(true){
            try{
                final Socket socket = sever.accept();
                new Thread(new Runnable(){
					public void run(){
						BufferedReader sockin;
						try {
							sockin = new BufferedReader(new InputStreamReader(
									socket.getInputStream(), "UTF-8"));
							PrintWriter out = new PrintWriter(
									socket.getOutputStream());
							while (!socket.isClosed()) {
								String msgRecv;
								msgRecv = sockin.readLine();
								out.println("Server received: " + msgRecv);
								out.flush();
						    	 
								if (msgRecv == null || msgRecv.equals("quittest"))
									break;
								//System.out.println(msgRecv);
							}
							socket.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}).start();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}